package com.csxy.lsh.controller;


import com.csxy.lsh.entity.vo.CoursePublishVo;
import com.csxy.lsh.entity.vo.CourseQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csxy.lsh.R;
import com.csxy.lsh.entity.CourseInfoForm;
import com.csxy.lsh.entity.EduCourse;
import com.csxy.lsh.entity.vo.CourseWebVo;
import com.csxy.lsh.entity.vo.EduCourseVo;
import com.csxy.lsh.service.EduCourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;

    @RequestMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoForm courseInfoForm) {
        //根据前端页面去做一个表单实体类，用于接收表单数据
        String id = eduCourseService.addCourseInfo(courseInfoForm);
        //回传ID主要是下一步创建章节时把章节和课程相关联
        return R.ok().data("courseId", id);
    }

    //查询课程信息列表

    @GetMapping("/getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable String courseId) {
        CourseInfoForm courseInfoForm = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoForm", courseInfoForm);
    }

    //查询课程信息列表
    @GetMapping({"/getCourseList", "/getCourseList/"})
    public R getCourseList() {
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list", list);
    }

    //修改课程信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm) {
        eduCourseService.updateCourseInfo(courseInfoForm);
        return R.ok();
    }

    //删除课程信息
    @DeleteMapping("/removeCourseById/{id}")
    public R removeCourseById(@PathVariable("id") String id) {
        boolean flag = eduCourseService.removeCourse(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @Operation(description = "多条件查询课程带分页")
    @PostMapping("/pageCourseCondition/{page}/{limit}")
    public R pageCourseCondition(
            @Parameter(name = "page", description = "当前页码", required = true)
            @PathVariable Long page,
            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable Long limit,
            @RequestBody(required = false) CourseQuery courseQuery) {

        Page<EduCourse> pageCourse = new Page<>(page, limit);
        eduCourseService.pageQuery(pageCourse, courseQuery);

        List<EduCourse> records = pageCourse.getRecords();
        Long total = pageCourse.getTotal();

        return R.ok().data("total", total).data("rows", records);
    }

    @GetMapping("/getpublishCourseInfo/{id}")
    public R getpublishCourseInfo(@PathVariable String id) {
        CoursePublishVo publishCourseInfo = eduCourseService.getPublishCourseInfo(id);
        return R.ok().data("publishCourse", publishCourseInfo);
    }

    @PostMapping("/getCourseInfoByIdOrder/{id}")
    public EduCourseVo getCourseInfoByIdOrder(@PathVariable("id") String courseId) {
        CourseWebVo courseInfo=eduCourseService.getBaseCourseInfo(courseId);
        EduCourseVo eduCourseVo=new EduCourseVo();
        BeanUtils.copyProperties(courseInfo,eduCourseVo);
        return eduCourseVo;
    }
    //发布课程
    @PostMapping("/publishCourse/{courseId}")
    public R publishCourse(@PathVariable("courseId") String courseId) {
        EduCourse before = eduCourseService.getById(courseId);
        System.out.println("发布前课程对象: " + before);
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        boolean flag = eduCourseService.updateById(eduCourse);
        System.out.println("发布课程ID: " + courseId + "，更新结果: " + flag);
        return flag ? R.ok() : R.error();
    }
}

